package ua.org.cityid.client.controls;

import ua.org.cityid.client.mvp.MainPresenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class NavBar extends Composite {
	@UiField
	Element countLabel;
	@UiField
	Anchor buttonNext;
	@UiField
	Anchor buttonPrev;

	private static NavBarUiBinder uiBinder = GWT.create(NavBarUiBinder.class);

	interface NavBarUiBinder extends UiBinder<Widget, NavBar> {
	}

	public NavBar() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void update(int startIndex, int count, int max) {
		setVisibility(buttonNext, startIndex != 0);
		setVisibility(buttonPrev, startIndex + MainPresenter.MAX_VISIBLE_REGIONS < count);
		int last = (startIndex + max);
		if (last > count) 
			last = count;
		countLabel.setInnerText((startIndex + 1) + " - " + last + " из " + count);
	}

	private void setVisibility(Widget widget, boolean visible) {
		widget.getElement().getStyle().setVisibility(
				visible ? Visibility.VISIBLE : Visibility.HIDDEN);
	}

	public HasClickHandlers getButtonNext() {
		return buttonNext;
	}
	
	public HasClickHandlers getButtonPrev() {
		return buttonPrev;
	}
}
